package co.uniquindio.software.medisys.logica;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;

import co.uniquindio.software.medisys.entidades.Eps;


public class GestorEPS 
{

	EntityManagerFactory emf = Persistence.createEntityManagerFactory("MedisysPU");
	

	public void guardarEPS(Eps	eps)
	{
		EntityManager em = emf.createEntityManager();
		EntityTransaction et = em.getTransaction();
		et.begin();
		em.persist(eps);
		em.flush();
		et.commit();
	}
	
	
	public void modificarEPS(Eps	eps)
	{
		EntityManager em = emf.createEntityManager();
		EntityTransaction et = em.getTransaction();
		et.begin();
		em.merge(eps);
		em.flush();
		et.commit();
	}
	
	public Vector<Eps> obtenerEps(){
		EntityManager em = emf.createEntityManager();
		return (Vector<Eps>) em.createQuery("SELECT e FROM Eps e").getResultList();
	}
	
	
	public Eps consultarEpsPorNombre(String nombre) {
		Eps eps;
		try {
	
			EntityManager em = emf.createEntityManager();
			eps = (Eps) em.createQuery("SELECT e FROM Eps e WHERE e.descripcion=:nombre")
					.setParameter("nombre", nombre).getSingleResult();
			if(eps != null){
				return eps;
			}else{
				return null;
			}
			
		} catch (NoResultException ex) {
			ex.printStackTrace();
			eps = null;
		}
		return eps;
	}
}
